package com.polytech.web.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.Session;

import com.polytech.business.ForumManager;
import com.polytech.model.Post;
import com.polytech.model.Topic;
import com.polytech.utils.BusinessFactory;
import com.polytech.web.exceptions.PermissionDeniedException;


public class ForumController extends HttpServlet {

	private ForumManager forumManager;
	@Override
	public void init() throws ServletException {
		super.init();
		this.forumManager=BusinessFactory.getForumManager();
	}
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
	
		String uri=request.getRequestURI();
		String actionKey=actionResolver(uri);
		String view="";
		if("list-topics".equals(actionKey)){
			view =listTopicsAction(request,response);
		}else if("new-topic".equals(actionKey)){
			try {
				view =newTopicAction(request,response);
			} catch (PermissionDeniedException e) {
//				e.printStackTrace();
			}
		}else if("show-topic".equals(actionKey)){
			view =showTopicAction(request,response);
		}else if("new-post".equals(actionKey)){
			try {
				view =newPostAction(request,response);
			} catch (PermissionDeniedException e) {
//				e.printStackTrace();
			}
		}
		else{
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		if(view.startsWith("/views"))
		  getServletContext().getRequestDispatcher(view).forward(request, response);
		else
		  response.sendRedirect(view);

	}
	private String newPostAction(HttpServletRequest request,HttpServletResponse response) throws PermissionDeniedException {
		
		if(request.getSession().getAttribute("login")==null)
			throw new PermissionDeniedException();
		int id=0;
		try{
			 id=Integer.parseInt(request.getParameter("id"));
		}catch (Exception e) {
			return "/irm-forum/404.jsp";
		}
		Topic topic = forumManager.findTopic(id);
		if(topic==null){
			return "/irm-forum/404.jsp";
		}
		String text=request.getParameter("text");
		String userName=(String) request.getSession().getAttribute("login");
		Post post=new Post();
		post.setPublicationDate(new Date());
		post.setText(text);
		forumManager.createPost(post, id, userName);
		return "/irm-forum/forum/show-topic.action?id="+id;
	}
	private String showTopicAction(HttpServletRequest request,HttpServletResponse response) {
		int id=0;
		try{
			 id=Integer.parseInt(request.getParameter("id"));
		}catch (Exception e) {
			return "/irm-forum/404.jsp";
		}
		Topic topic = forumManager.findTopic(id);
		if(topic==null){
			return "/irm-forum/404.jsp";
		}
		request.setAttribute("topic",topic);
		topic.setPosts(forumManager.findAllTopicPosts(id));
		return "/views/topic.jsp";
	}
	private String newTopicAction(HttpServletRequest request,HttpServletResponse response) throws PermissionDeniedException {
		if(request.getSession().getAttribute("login")==null)
			throw new PermissionDeniedException();
		String subject=request.getParameter("subject");
		String text=request.getParameter("text");
		if(subject==null && text==null){
			return "/views/new-topic.jsp";
		}
		Topic topic=new Topic();
		topic.setText(text);
		topic.setSubject(subject);
		topic.setPublicationDate(new Date());
		String login=(String) request.getSession().getAttribute("login");
		forumManager.createTopic(topic,login);
		return "/irm-forum/forum/list-topics.action";
	}
	private String listTopicsAction(HttpServletRequest request,HttpServletResponse response) {
		List<Topic> listTopics = forumManager.findAllTopics();
		request.setAttribute("listTopics", listTopics);
		return "/views/list-topics.jsp";
	}
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		doGet(req, resp);
	}
	private String  actionResolver(String uri){
		int i=uri.lastIndexOf("/")+1;
		int j=uri.lastIndexOf(".");
		String key="";
		if(j>0){
			key=uri.substring(i,j);
		}
	   return key;
	}
}
